/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gad.visualmodule;

import java.io.Serializable;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.gad.model.CompositeStep;
import org.gad.model.ExampleStep;
import org.gad.model.IStepStateListener;
import org.gad.model.SequenceStep;
import org.gad.model.SplitStep;
import org.gad.model.Step;
import org.gad.ui.BoxScene;
import org.gad.ui.SceneFactory;
import org.gad.ui.StepWidgetBase;
import org.netbeans.api.visual.action.ActionFactory;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.Utilities;
/**
 * Top component which displays something.
 */
final class boxViewTopComponent extends TopComponent
{

    private static boxViewTopComponent instance;
    private static BoxScene _scene;
    private static Step _rootStep;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "boxViewTopComponent";

    private boxViewTopComponent()
    {
        initComponents();
        setName(NbBundle.getMessage(boxViewTopComponent.class,
                "CTL_boxViewTopComponent"));
        setToolTipText(NbBundle.getMessage(boxViewTopComponent.class,
                "HINT_boxViewTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        /////////////////////////////////////////////////////////////////////////
        _scene = new BoxScene();

        _rootStep = GenerateHugeTree();
        SplitStep ss = new SplitStep("Split");
        ss.AddStep(new ExampleStep("Crossing-over"));
        ((CompositeStep) _rootStep).AddStep(ss);
        StepWidgetBase swb = new SceneFactory().CreateWidgets(_scene, _rootStep);
        _scene.getMainLayer().addChild(swb);

        _scene.getActions().addAction(ActionFactory.createZoomAction());
        _scene.getActions().addAction(ActionFactory.createPanAction());


        _rootStep.setStepChangeListener(new IStepStateListener()
        {

            public void StepStateChanged(Step step)
            {
                regenerateScene(_scene);
            }
        });
        jScrollPane1.setViewportView(_scene.createView());
    }

    private static void regenerateScene(BoxScene scene)
    {
        scene.clear();
        StepWidgetBase swb = new SceneFactory().CreateWidgets(scene, _rootStep);
        _scene.getMainLayer().addChild(swb);
    }

    public static CompositeStep GenerateSeq()
    {
        SequenceStep result = new SequenceStep("Sequence1");
        result.AddStep(new ExampleStep("Crossing-over"));
        result.AddStep(new ExampleStep("Mutation"));
        result.AddStep(new ExampleStep("Crossing-over"));
        result.AddStep(new ExampleStep("Inversion"));
        result.AddStep(new ExampleStep("Mutation"));
        result.AddStep(new ExampleStep("Crossing-over"));
        return result;
    }

    public static CompositeStep GenerateSplit()
    {
        SplitStep result = new SplitStep("Split1");
        result.AddStep(new ExampleStep("Crossing-over"));
        result.AddStep(new ExampleStep("Mutation"));
        result.AddStep(new ExampleStep("Crossing-over"));
        result.AddStep(new ExampleStep("Inversion"));
        return result;
    }

    public static CompositeStep GenerateBigTree()
    {
        SequenceStep result = new SequenceStep("Sequence2");

        result.AddStep(GenerateSplit());
        result.AddStep(GenerateSplit());

        result.AddStep(new ExampleStep("Crossing-over"));

        result.AddStep(new ExampleStep("Crossing-over"));
        result.AddStep(GenerateSplit());
        result.AddStep(GenerateSplit());
        return result;
    }

    public static CompositeStep GenerateHugeTree()
    {
        SplitStep result = new SplitStep("Split2");
        result.AddStep(GenerateBigTree());
        result.AddStep(GenerateSeq());

        result.AddStep(GenerateBigTree());
        return result;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized boxViewTopComponent getDefault()
    {
        if (instance == null)
        {
            instance = new boxViewTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the boxViewTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized boxViewTopComponent findInstance()
    {
        TopComponent win = WindowManager.getDefault().findTopComponent(
                PREFERRED_ID);
        if (win == null)
        {
            Logger.getLogger(boxViewTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof boxViewTopComponent)
        {
            return (boxViewTopComponent) win;
        }
        Logger.getLogger(boxViewTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType()
    {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace()
    {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID()
    {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable
    {

        private static final long serialVersionUID = 1L;

        public Object readResolve()
        {
            return boxViewTopComponent.getDefault();
        }
    }

}
